package com.etime.shycourse.services.account.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.dao.SettlementMapper;
import com.etime.shycourse.dto.CourseSellConditionDto;
import com.etime.shycourse.dto.SettlementDto;
import com.etime.shycourse.services.account.interfaces.AccountStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author hw
 * @date 2022/7/18 16:38
 */
@Service
public class AccountStatisticsServiceImpl implements AccountStatisticsService {

    @Autowired
    private SettlementMapper settlementMapper;

    // 根据时间段以及结算类型查询结算记录
    @Override
    public Page<SettlementDto> selectAllSettlement(String settlementType, String startDate, String endDate,String pagenum,String pagesize) {
        Integer pageNum = Integer.parseInt(pagenum);
        Integer pageSize = Integer.parseInt(pagesize);
        Page<SettlementDto> page = new Page<>(pageNum,pageSize);
        Page<SettlementDto> settlementDtoPage = settlementMapper.selectAllSettlement(settlementType,startDate, endDate,page);
        for (SettlementDto settlementDto : settlementDtoPage.getRecords()) {
            String username = settlementMapper.getSettlementUsername(settlementDto.getSettlementuserid(),settlementDto.getSettlementtype());
            settlementDto.setUsername(username);
            settlementDto.setCalStartDate(startDate);
            settlementDto.setCalEndDate(endDate);
        }
        return settlementDtoPage;
    }


    // 查询结算记录明细
    @Override
    public Page<SettlementDto> selectSettlementDetail(String settlementtype,String startDate,String endDate,String settlementuserid,String pagenum,String pagesize) {
        Integer pageNum = Integer.parseInt(pagenum);
        Integer pageSize = Integer.parseInt(pagesize);
        Page<SettlementDto> page = new Page<>(pageNum,pageSize);
        String username = settlementMapper.getSettlementUsername(Integer.parseInt(settlementuserid),Integer.parseInt(settlementtype));
        Page<SettlementDto> page1 = settlementMapper.selectSettlementDetail(settlementtype,startDate, endDate, settlementuserid,page);
        for (SettlementDto record : page1.getRecords()) {
            record.setUsername(username);
        }
        return page1;
    }


    // 按照课程类型分组查询课程销售情况
    @Override
    public Page<CourseSellConditionDto> selectAllCourse(String coursetypeid,String pagenum,String pagesize) {
        Integer pageNum = Integer.parseInt(pagenum);
        Integer pageSize = Integer.parseInt(pagesize);
        Page<CourseSellConditionDto> page = new Page<>(pageNum,pageSize);
        QueryWrapper<String> wrapper = new QueryWrapper<>();
        wrapper.eq(!coursetypeid.equals("-1000"),"course.coursetypeid",coursetypeid);
        return settlementMapper.selectAllCourse(wrapper,page);
    }

    // 查询课程销售明细
    @Override
    public Page<SettlementDto> selectCourseDetail(String courseid,String pagenum,String pagesize) {
        Integer pageNum = Integer.parseInt(pagenum);
        Integer pageSize = Integer.parseInt(pagesize);
        Page<SettlementDto> page = new Page<>(pageNum,pageSize);
        Page<SettlementDto> settlementDtoPage= settlementMapper.selectCourseDetail(courseid,page);
        for (SettlementDto record : settlementDtoPage.getRecords()) {
            record.setUsername(settlementMapper.getSettlementUsername(record.getSettlementuserid(),record.getSettlementtype()));
        }
        return settlementDtoPage;
    }
}
